package com.yz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yz.common.CustomException;
import com.yz.entity.Category;
import com.yz.entity.Dish;
import com.yz.entity.Setmeal;
import com.yz.mapper.CategoryMapper;
import com.yz.service.CategoryService;
import com.yz.service.DishService;
import com.yz.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public boolean removeById(Serializable id) {
        // 查看当前菜品分类是否关联了菜品
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(dishLambdaQueryWrapper);
        if(count1>0){
            //抛出已关联菜品异常
            throw new CustomException("该分类已关联菜品，请先删除菜品，再删除分类");
        }

        // 查看当前菜品分类是否关联了菜品
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(setmealLambdaQueryWrapper);
        if(count2>0){
            //抛出已关联套餐异常
            throw new CustomException("该分类已关联套餐，请先删除菜品，再删除分类");
        }
        return super.removeById(id);
    }
}
